<template>
	<view class="my_tally">
		<uv-navbar leftIcon="arrow-leftward" leftIconColor="#FFF" placeholder autoBack bgColor="#161714">
			<template v-slot:center>
				<view class="center_slot">我的众筹</view>
			</template>
		</uv-navbar>
		<view class="list">
			<view class="item" v-for="item in _this.list">
				<view class="label">{{item.status_name}}</view>
				<view class="item_view">
					<image :src="item.crowdfunding.cover" class="cover"></image>
					<view class="info_view">
						<view class="name">{{item.crowdfunding.name}}</view>
						<view class="cell">参与时间：{{item.created_at}}</view>
						<view class="cell">参与金额：${{item.total}}</view>
						<view class="cell">总利息：${{item.interest}}</view>
					</view>
				</view>
				<view class="btn_view" v-if="item.status==0">
					<view>剩余{{item.surplus_day}}天</view>
				</view>
				<view class="btn_view" v-if="item.status==1">
					<view @click="retrieve(item.id)">取回本金</view>
				</view>
			</view>
			<uv-load-more :status="_this.loadmore_status" line @loadmore="loadmore" />
		</view>
	</view>
</template>

<script setup>
	import {
		crowdfundingJoinList,
		crowdfundingRetrieve
	} from "@/request/api";
	import {
		onReachBottom
	} from "@dcloudio/uni-app";
	import useList from "@/hooks/useList.js";
	const {
		_this
	} = useList();
	const getList = async () => {
		let {
			list
		} = await crowdfundingJoinList({
			page: _this.page,
			limit: _this.limit,
		});
		_this.last_page = list.last_page;
		list = list.list;
		_this.list = _this.page == 1 ? list : [..._this.list, ...list];
		_this.loadmore_status = _this.last_page == _this.page ? 'nomore' : 'loadmore';
	};
	getList();
	const loadmore = () => {
		if (_this.page >= _this.last_page) return;
		_this.page++;
		getList();
	};
	onReachBottom(() => loadmore());

	const retrieve = async (id) => {
		await crowdfundingRetrieve({
			id
		});
		uni.$uv.toast("操作成功");
		setTimeout(() => {
			_this.page = 1;
			_this.list = [];
			getList();
		}, 1000);
	};
</script>

<style scoped lang="scss">
	.my_tally {
		.center_slot {
			color: #FFFFFF;
			font-size: 18px;
			font-weight: bold;
		}

		.list {
			padding: 0 20rpx;
			box-sizing: border-box;

			.item {
				border-radius: 20rpx;
				margin-bottom: 40rpx;
				padding: 30rpx 30rpx 0;
				box-sizing: border-box;
				background: #2E2E2E;
				position: relative;

				.label {
					width: 110rpx;
					height: 42rpx;
					line-height: 42rpx;
					text-align: center;
					color: #333333;
					font-size: 20rpx;
					background: #DDF247;
					border-radius: 0rpx 21rpx 21rpx 0rpx;
					position: absolute;
					left: 0;
					top: 50rpx;
					z-index: 2;
				}

				.item_view {
					padding-bottom: 24rpx;
					box-sizing: border-box;
					display: flex;

					.cover {
						width: 200rpx;
						height: 200rpx;
					}

					.info_view {
						flex: 1;
						padding-top: 10rpx;
						padding-left: 30rpx;
						box-sizing: border-box;
						overflow: hidden;

						.name {
							color: #FFFFFF;
							font-size: 32rpx;
							font-weight: 800;
						}

						.cell {
							color: #999999;
							font-size: 26rpx;
							margin-top: 20rpx;
						}
					}
				}

				.btn_view {
					border-top: 1px solid #444444;
					padding: 20rpx 0 28rpx;
					box-sizing: border-box;
					display: flex;
					justify-content: flex-end;

					view {
						width: 190rpx;
						height: 60rpx;
						line-height: 60rpx;
						text-align: center;
						color: #CCCCCC;
						font-size: 28rpx;
						border-radius: 10rpx;
						background: #444444;
					}
				}
			}
		}
	}
</style>